package top.yunmouren.atom.webui.cmd;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.TextComponent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import top.yunmouren.atom.webui.socket.server;
import top.yunmouren.atom.webui.tools.socket;

import java.io.IOException;
import java.net.Socket;

@Mod.EventBusSubscriber
public class atomCommandEventHandler {
    static CommandDispatcher<CommandSourceStack> dispatcher = null;

    @SubscribeEvent
    public static void onServerStaring(RegisterCommandsEvent event) {
        dispatcher = event.getDispatcher();
        new socketConnect();
        new socketServer();
        new socketServerSendMsg();
        new socketClientSendMsg();
    }

    public static void registerCommands(String ModID, String CommandPrefix, String CommandOptions, Command<CommandSourceStack> CommandInstance, int Authority) {
        LiteralCommandNode<CommandSourceStack> cmd = dispatcher.register(
                Commands.literal(ModID)
                        .then(Commands.literal(CommandOptions)
                                .requires((commandSource) -> commandSource.hasPermission(Authority))
                                .executes(CommandInstance)));
        dispatcher.register(Commands.literal(CommandPrefix).redirect(cmd));
    }
    public static socket.socketClient socket = null;
    public static class socketConnect implements Command<CommandSourceStack> {

        public socketConnect() {
            registerCommands("atom", "atom", "socketConnect", this, 0);
        }
        @Override
        public int run(CommandContext<CommandSourceStack> context) {
            new Thread(() -> {
                try {
                    socket = new socket.socketClient(new Socket("localhost", 9001));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
            return 0;
        }
    }
    public static class socketClientSendMsg implements Command<CommandSourceStack> {
        public socketClientSendMsg() {
            registerCommands("atom", "atom", "socketClientSendMsg", this, 0);
        }
        @Override
        public int run(CommandContext<CommandSourceStack> context) {
            try {
                socket.clientSendsMessage("newbee");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }
    public static class socketServer implements Command<CommandSourceStack> {
        public socketServer() {
            registerCommands("atom", "atom", "socketServer", this, 0);
        }
        @Override
        public int run(CommandContext<CommandSourceStack> context) {
            new server().start();
            return 0;
        }
    }
    public static class socketServerSendMsg implements Command<CommandSourceStack> {
        public socketServerSendMsg() {
            registerCommands("atom", "atom", "socketServerSendMsg", this, 0);
        }
        @Override
        public int run(CommandContext<CommandSourceStack> context) {
            try {
                server.server.socketClient.clientSendsMessage("hello");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }
}
